package com.xneure.code.excel;

import java.io.InputStream;
import java.util.Iterator;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.xneure.code.exception.ExcelParseException;
import com.xneure.code.exception.ExceptionLevel;
import com.xneure.code.exception.ExceptionReport;
import com.xneure.code.exception.Report;
import com.xneure.code.upload.Handling;

public class ExcelImportProvide{

	ExcelImportHandler _Handler;
	public static FastDateFormat sdf = FastDateFormat.getInstance("yyyy-MM-dd");
	static FastDateFormat sdf2 = FastDateFormat.getInstance("yyyy年MM月");
	static FastDateFormat sdf3 = FastDateFormat.getInstance("yyyy年MM月dd日");
	private ExceptionReport report;

	public ExcelImportProvide(ExcelImportHandler eHandler){
		_Handler = eHandler;
		report = new ExceptionReport();
	}

	public void loadData(Object excel) throws ExcelParseException{
		try{
			Workbook wb = null;
			InputStream excel2 = (InputStream)excel;
			wb =  WorkbookFactory.create(excel2);
			int countSheet = wb.getNumberOfSheets();
			Sheet sheet;
			_Handler.setSheetCount(countSheet);
			for(int i = 0;i < countSheet;i++){
				try{
					sheet = wb.getSheetAt(i);
					_Handler.start(sheet.getSheetName(),i + 1);
					Iterator<Row> rows = sheet.rowIterator();
					int rowCount = sheet.getLastRowNum();
					_Handler.setRowCount(rowCount);
					while(rows.hasNext()){
						Row row = rows.next();
						if(_Handler.skipRow(row.getRowNum())){
							continue;
						}
						int colCount = row.getLastCellNum();
						_Handler.setColCount(rowCount,colCount);
						try{
							_Handler.rowHandler(getColData(row.cellIterator(),colCount),row.getRowNum());
						}catch(ExcelParseException e){
							if(_Handler.getHandling() == Handling.IGNORE){
								report.addReport(new Report(ExceptionLevel.WARNING,e.getMessage()));
							}else{
								throw e;
							}
						}
					}
					_Handler.end();
				}catch(Exception e){
					if(_Handler.getHandling() == Handling.IGNORE){
						report.addReport(new Report(ExceptionLevel.WARNING,e.getMessage()));
					}else{
						throw e;
					}
				}
			}
			_Handler.finish();
			if(report.isError()){
				throw new ExcelParseException(report.getReport());
			}
		}catch(Exception e){
			e.printStackTrace();
			if(e.getClass() == ExcelParseException.class){
				throw (ExcelParseException)e;
			}else{
				throw new ExcelParseException("解析模板文件时发生错误，请确认是否为有效的模板文件。\n".concat(e.getCause() != null ? e.getCause().getMessage() : ""));
			}
		}
	}

	private Object[] getColData(Iterator<Cell> cells,int cols){
		Object[] val = new Object[cols];
		while(cells.hasNext()){
			Cell cell = cells.next();
			Object data = "";
			switch(cell.getCellType()){
				case HSSFCell.CELL_TYPE_NUMERIC:
					if(HSSFDateUtil.isCellDateFormatted(cell)){
						data = cell.getDateCellValue();
					}else if(cell.getCellStyle().getDataFormat() == 184 || cell.getCellStyle().getDataFormat() == 57){
						data = cell.getDateCellValue();
					}else if(cell.getCellStyle().getDataFormat() == 31){
						data = cell.getDateCellValue();
					}else{
						data = cell.getNumericCellValue();
					}
					break;
				case HSSFCell.CELL_TYPE_STRING:
					data = cell.getStringCellValue();
					break;
				case HSSFCell.CELL_TYPE_BOOLEAN:
					data = cell.getBooleanCellValue();
					break;
				case HSSFCell.CELL_TYPE_FORMULA:// 公式
					data = cell.getCellFormula();
					break;
				default:
					break;
			}
			val[cell.getColumnIndex()] = data;
		}
		return val;
	}
	
	
	public ExcelImportHandler getHandler(){
		return _Handler;
	}

	public void saveData(){
	}

	public ExceptionReport getReport() {
		return report;
	}
	
}
